#!/usr/bin/env python
# -*- coding: UTF-8 -*-

'''
Copyright (C) 2011 dbzhang800@gmail.com
All rights reserved.
'''
import math
try:
    from PySi0de import QtCore, QtGui
except ImportError:
    from PyQt4 import QtCore, QtGui

class Widget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)
        self.setWindowTitle(u"李帅计算 2011.03.03")
        self.angleEdit = QtGui.QDoubleSpinBox()
        self.angleEdit.setValue(10)
        self.angleEdit.setSuffix(u' °')
        self.totalEdit = QtGui.QSpinBox()
        self.totalEdit.setValue(40)
        self.dEnergyEdit = QtGui.QDoubleSpinBox()
        self.dEnergyEdit.setValue(0.5)
        self.fileNameEdit = QtGui.QLineEdit()
        self.fileNameEdit.setText("b-focus.txt")
        self.okButton = QtGui.QPushButton(u"确定")
        self.okButton.setFocus()
        form = QtGui.QFormLayout()
        form.addRow(u"角度",  self.angleEdit)
        form.addRow(u"数目",  self.totalEdit)
        form.addRow(u"能散", self.dEnergyEdit)
        form.addRow(u"文件名", self.fileNameEdit)
        form.addRow(u"",  self.okButton)
        self.setLayout(form)
        self.okButton.clicked.connect(self.onCalc)

    def onCalc(self):
        anglelist = []
        angle = self.angleEdit.value() / 180 * math.pi
        N = self.totalEdit.value()
        for i in range(N):
            a = math.tan(angle*(i+1)/N)
            anglelist.append(a)

        with file(self.fileNameEdit.text(), "w") as f:
            d = self.dEnergyEdit.value()
            for dE in [0, d/2, -d/2]:
                for i in range(N):
                    f.write("%-4i  0     0   %15.9f   0   %10.4f\n"
                            % (i+1, anglelist[i], dE))



if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())
